package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

//@Component//交给Spring框架管理
@Service
public class BookServiceImpl implements BookService
/*, InitializingBean, DisposableBean */
{
   /* //删除业务层中使用new的方式创建的dao对象
 private BookDao bookDao;
    private UserDao userDao;
    public void save() {
        System.out.println("book service save ...");
        bookDao.save();
        userDao.save();
    }
    //提供对应的set方法
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }


public void afterPropertiesSet() throws Exception {
        System.out.println("service init");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("service destory");
    }


    //构造器方法注入
 private BookDao bookDao;
    public BookServiceImpl(BookDao bookDao){
        this.bookDao = bookDao;
    }
    public void save(){
        System.out.println("book service save...");
        bookDao.save();
    }


  //在BookServiceImpl声明userDao并提供多个参数的构造函数
    private BookDao bookDao;
    private UserDao userDao;
    public BookServiceImpl(BookDao bookDao,UserDao userDao){
        this.bookDao = bookDao;
        this.userDao = userDao;
    }
    public void save(){
        System.out.println("book service save...");
        bookDao.save();
        userDao.save();
    }


 private BookDao bookDao;
    public void setBookDao(BookDao bookDao){
        this.bookDao = bookDao;
    }
    public void save(){
        System.out.println("book service save...");
        bookDao.save();
    }


 //注解开发
    public void save(){
        System.out.println("book service save ...");
    }


//*
/注解开发定义bean
    private BookDao bookDao;
    public void setBookDao(BookDao bookDao){
        this.bookDao = bookDao;
    }
    public void save(){
        System.out.println("book service save ...");
        bookDao.save();
    }
*/

 //注解开发依赖注入
    //Autowired为引用类型属性设置值
    @Autowired
    //Qualifier注解后的值就是需要注入的bean的名称
    //@Qualifier不能单独使用，必须和@Autowired一起使用
    @Qualifier("bookDao")
    private BookDao bookDao;
//    public void setBookDao(BookDao bookDao){
 //       this.bookDao = bookDao;
 //   }
    public void save(){
        System.out.println("book service save ...");
     //   bookDao.save();
    }

}
